package com.db.utility;

import java.lang.reflect.ParameterizedType;
import java.util.LinkedList;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.transaction.annotation.Transactional;

@Transactional
public abstract class BaseDaoImpl<T> implements BaseDao<T>{
	
	@Resource
	private SessionFactory sessionFactory;
	
	//泛型策略
	private T entityClass;
	
	protected HibernateTemplate hTemplate = null;
	
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	public Session getSession () {
		return sessionFactory.getCurrentSession();
	}
	
	public BaseDaoImpl() {
		//entityClass =(T) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
		//gethTemplate();
		m_msg = new LinkedList<String>();
		m_errors = new LinkedList<String>();
	}
	
	public  HibernateTemplate gethTemplate() {
		if(hTemplate == null) {
			synchronized(this) {	
				hTemplate = new HibernateTemplate(sessionFactory);
			}
		}
			
		return hTemplate;
	}
	
	String getMessage(String[] msg) {
		if(msg.length == 0)
			return new String();
		else
			return msg[msg.length -1];
	}


	@SuppressWarnings("unchecked")
	@Override
	public T findByName(String name) {
		return (T) gethTemplate().find("from " + ((Class<T>) entityClass).getName() + "where name=?",name).get(0);
		
	}

	@Override
	public T findById(int id) {
		return this.hTemplate.get((Class<T>) entityClass, id);
	}

	@Override
	public List<T> findAll() {
		try{
		 Class<T> entityClass =(Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
		 List<T> ret = gethTemplate().find("from " + entityClass.getName());
		 m_msg.add("success");
		 return ret;
		} catch(Exception e) {
			m_errors.add(e.toString());
			return null;
		}
	}

	@Override
	public List<T> findByCondition(String condition) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<T> findByProperty() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int deleteByName(String name) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public boolean deleteById(int id) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public int deleteByIds(int[] ids) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int deleteByNames(String[] names) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int update(T t) {

		return 0;
	}

	@Override
	public int update(T[] ts) {

		return 0;
	}

	@Override
	public boolean save(T t) {
		try{
			gethTemplate().save(t);
			return true;
		} catch (Exception e){
			m_errors.add(e.toString());
			return false;
		}
	}

	@Override
	public int save(T[] ts) {

		return 0;
	}

	@Override
	public String getLastMessage() {
		return m_msg.get(m_msg.size() - 1);
	}

	@Override
	public String getLastError() {
		return m_msg.get(m_errors.size() - 1);
	}

	@Override
	public List<String> getAllMessages() {
		return m_msg;
	}

	@Override
	public List<String> getAllErrors() {
		return m_errors;
	}

	@Override
	public T findByUuid(String uuid) {
		gethTemplate().find("from " + ((Class<T>) entityClass).getName());
		return null;
	}
	
	public List<String> m_msg = null;
	
	public List<String> m_errors = null;
}
